package com.zlzxm.shoesmarket.viewmodel;

import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.zlzxm.shoesmarket.globle.UserManager;
import com.zlzxm.shoesmarket.net.BaseCallBack;
import com.zlzxm.shoesmarket.net.RequestCode;
import com.zlzxm.shoesmarket.net.RequestTip;
import com.zlzxm.shoesmarket.net.repository.ImageUploadRepository;
import com.zlzxm.shoesmarket.net.repository.RecruitRepository;
import com.zlzxm.shoesmarket.net.request.RqReleaseRecruit;
import com.zlzxm.shoesmarket.net.respone.BaseResponse;
import com.zlzxm.shoesmarket.util.RxAndroidHelp;
import com.zlzxm.shoesmarket.util.ZFileUtil;
import com.zlzxm.shoesmarket.view.window.wheelpick.EntityOption;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.core.ObservableSource;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.functions.Function;

/**
 * CREATE BY zlzxm ON 5/11/21
 */
public class VMReleaseRecruit extends BaseViewModel {

    private static final String TAG = "VMReleaseRecruit";

    ImageUploadRepository imageUploadRepository = new ImageUploadRepository();

    RecruitRepository recruitRepository = new RecruitRepository();

    public List<EntityOption> source = new ArrayList<>();

    private final MutableLiveData<String> liveSelectPicturePath = new MutableLiveData<>();

    public final MutableLiveData<Spanned> liveReleaseDesc = new MutableLiveData<>();

    public final MutableLiveData<EntityOption> liveSelectSource = new MutableLiveData<>();


    public VMReleaseRecruit() {

        createSource();
    }


    public MutableLiveData<String> getLiveSelectPicturePath() {
        return liveSelectPicturePath;
    }

    public void release(String content){

        if(!UserManager.getInstance().isLogin()){

            liveWarningToast.setValue(UserManager.TIP);

            return;
        }

        String path = liveSelectPicturePath.getValue();

        String source = liveSelectSource.getValue() == null?null:liveSelectSource.getValue().getId();

        if(TextUtils.isEmpty(path)){

            liveWarningToast.setValue("请先上传图片");

            return;
        }

        if(TextUtils.isEmpty(source)){

            liveWarningToast.setValue("请选择货源");

            return;
        }


        if(TextUtils.isEmpty(content)){

            liveWarningToast.setValue("请填写完整后再提交");

            return;
        }

        if (ZFileUtil.isFileExists(path)){

            imageUploadRepository.upload(new File(path))
                    .flatMap((Function<String, ObservableSource<BaseResponse<String>>>) url -> {
                        if(!TextUtils.isEmpty(url)){

                            return recruitRepository.releaseRecruit(new RqReleaseRecruit(
                                    content,
                                    url,
                                    Integer.valueOf(source),
                                    Integer.valueOf(UserManager.getInstance().getId())
                            ));
                        }

                        return null;
                    })
                    .compose(RxAndroidHelp.turn())
                    .subscribe(new BaseCallBack<String>() {

                        @Override
                        protected void succeed(String msg,String data) {

                            liveSucceedToast.setValue(msg);
                        }

                        @Override
                        protected void failed(String msg) {

                            liveErrorToast.setValue(msg);
                        }

                        @Override
                        protected void notLogin() {

                            liveNeedLogin.setValue(true);
                        }
                    }, throwable -> {
                        Log.e(TAG,throwable.getMessage());
                        liveErrorToast.setValue(RequestTip.REQUEST_ERROR);
                    });



        }else {

            liveWarningToast.setValue("图片不存在");

        }




    }

    public void requestReleaseDesc(){

        recruitRepository.releaseDesc()
                .compose(RxAndroidHelp.turn())
                .subscribe(new Consumer<BaseResponse<String>>() {
                    @Override
                    public void accept(BaseResponse<String> stringBaseResponse) throws Throwable {

                        if (stringBaseResponse.getCode() == RequestCode.REQUEST_SUCCEED){

                            liveReleaseDesc.setValue(Html.fromHtml(stringBaseResponse.getData()));
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Throwable {

                    }
                });



    }

    private void createSource() {

        source.add(new EntityOption("1","国现"));
        source.add(new EntityOption("2","美国"));
        source.add(new EntityOption("3","日韩"));
        source.add(new EntityOption("4","欧洲"));
        source.add(new EntityOption("5","其他"));

    }


}
